package org.lfh.wintersun.web.secondKill.controller;

import org.lfh.wintersun.web.secondKill.entity.SecondKillOrderEntity;
import org.lfh.wintersun.web.secondKill.service.SecondKillOrderService;
import org.lfh.wintersun.common.base.PageUtils;
import org.lfh.wintersun.common.result.CommonResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

import java.util.List;

/**
 * (SecondKillOrder)表控制层
 *
 * @author lfh
 * @since 2025-04-12 22:36:31
 */
@RestController
@RequestMapping("/secondKillOrder")
public class SecondKillOrderController {

    /**
     * 服务对象
     */
    @Resource
    private SecondKillOrderService secondKillOrderService;

    /**
     * 分页查询
     *
     * @param pageNum 页码
     * @param size    每页数量
     * @return 查询结果
     */
    @GetMapping("/queryByPage")
    public CommonResult<PageUtils<SecondKillOrderEntity>> queryByPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return CommonResult.success(secondKillOrderService.queryByPage(pageNum, size));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getById")
    public CommonResult<SecondKillOrderEntity> getById(@RequestParam Long id) {
        return CommonResult.success(secondKillOrderService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param secondKillOrderEntity 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public CommonResult<SecondKillOrderEntity> save(@RequestBody SecondKillOrderEntity secondKillOrderEntity) {
        return CommonResult.success(secondKillOrderService.insert(secondKillOrderEntity));
    }

    /**
     * 编辑数据
     *
     * @param secondKillOrderEntity 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public CommonResult<SecondKillOrderEntity> update(@RequestBody SecondKillOrderEntity secondKillOrderEntity) {
        return CommonResult.success(secondKillOrderService.update(secondKillOrderEntity));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/deleteById")
    public CommonResult<Boolean> deleteById(@RequestParam Long id) {
        this.secondKillOrderService.deleteById(id);
        return CommonResult.success();
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键数组
     * @return 删除是否成功
     */
    @GetMapping("/deleteIds")
    public CommonResult<Boolean> deleteIds(@RequestParam List<Long> ids) {
        secondKillOrderService.deleteIds(ids);
        return CommonResult.success();
    }

    @GetMapping("/secondKill")
    public CommonResult<SecondKillOrderEntity> secondKill(@RequestParam Long voucherId) {
        return CommonResult.success(secondKillOrderService.secondKill(voucherId));
    }
}
